using System;
using System.Collections;

namespace Jonathan.SharpDB
{
    public class TableCollection : CollectionBase
    {
        public TableCollection()
        {
        }

        #region Add

        /// <summary>
        /// Adds a Table to the collection...
        /// </summary>
        /// <param name="table">Table to add...</param>
        /// <returns>Added Table's index</returns>
        public int Add(Table table)
        {
            // Make sure the table is not null...
            if (table == null)
            {
                throw new ArgumentNullException("Table", "Table is null!");
            }

            // Add the table...
            int index = this.List.Add(table);

            return index;
        }

        /// <summary>
        /// Adds a range of Tables to the Collection...
        /// </summary>
        /// <param name="tables">Array of Table's to add...</param>
        public void AddRange(Table[] tables)
        {
            // Make sure the tables are not null...
            if (tables == null)
            {
                throw new ArgumentNullException("Table", "Tables are null!");
            }

            // Add each table...
            for (int a = 0; a < tables.Length; a++)
            {
                this.Add(tables[a]);
            }
        }

        #endregion

        #region Remove

        /// <summary>
        /// Removes a Table from the Collection...
        /// </summary>
        /// <param name="table">Table to remove...</param>
        public void Remove(Table table)
        {
            // Get the index of the table...
            int index = this.IndexOf(table);

            // Make sure it is a valid index...
            if (index != -1)
            {
                // Remove Table...
                this.RemoveAt(index);
            }
        }

        /// <summary>
        /// Removes a range of Tables from the collection...
        /// </summary>
        /// <param name="tables">Array of Tables to be removed...</param>
        public void RemoveRange(Table[] tables)
        {
            if (tables == null)
            {
                throw new ArgumentNullException("Table", "Tables are null!");
            }

            for (int a = 0; a < tables.Length; a++)
            {
                this.Remove(tables[a]);
            }
        }

        /// <summary>
        /// Removes all Tables from the collection...
        /// </summary>
        public new void Clear()
        {
            if (this.Count == 0)
            {
                // Nothing to do, return...
                return;
            }

            // Clear the list...
            base.Clear();

            // Rest the Capacity...
            this.InnerList.Capacity = 0;
        }

        /// <summary>
        /// Gets the index of a specified Table...
        /// </summary>
        /// <param name="table">Table to get index for...</param>
        /// <returns>Index of Table</returns>
        public int IndexOf(Table table)
        {
            // Look for the Table...
            for (int a = 0; a < this.Count; a++)
            {
                if (this[a] == table)
                {
                    // Found it...
                    return a;
                }
            }

            // Not found...
            return -1;
        }

        /// <summary>
        /// Gets the index of a specified Table...
        /// </summary>
        /// <param name="table">Table to get index for...</param>
        /// <returns>Index of Table</returns>
        public int IndexOf(string tableName)
        {
            // Look for the Table...
            for (int a = 0; a < this.Count; a++)
            {
                if (this[a].Name.Equals(tableName, StringComparison.CurrentCultureIgnoreCase))
                {
                    // Found it...
                    return a;
                }
            }

            // Not found...
            return -1;
        }

        internal void SetTable(int index, Table tbl)
        {
            this.List[index] = tbl;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets a Table at the requested Index...
        /// </summary>
        /// <param name="index">Index of Table...</param>
        /// <returns>Table at requested Index...</returns>
        public Table this[int index]
        {
            get
            {
                // Make sure it is a valid index...
                if (index < 0 || index >= this.Count)
                {
                    return null;
                }

                // Return the tabber table...
                return this.List[index] as Table;
            }
        }

        /// <summary>
        /// Gets a Table at the requested Index...
        /// </summary>
        /// <param name="tableName">Name of Table...</param>
        /// <returns>Table at requested Index...</returns>
        public Table this[string tableName]
        {
            get
            {
                return this[IndexOf(tableName)];
            }
        }

        #endregion
    }
}
